<?php

namespace app\controller;

use app\model\Role;
use app\model\Permission;
use app\validator\RoleAssignPerms;
use app\BaseController;
use think\Request;

class RolePermissionController extends  BaseController{
    public function assign_permissions(Request $request){
        $data = $request->param();

        //  验证参数
        $validate = new RoleAssignPerms();
        if (!$validate->check($data)) {
            return json([
                'code' => 400,
                'msg' => $validate->getError()
            ], 400);
        }

        $perm_id = $data['perm_id'];
        $role_id = $data['role_id'];
        $role = Role::find($role_id);
        if (!$role)
            return json([
                'code' => 404,
                'msg' => '添加失败'
            ],404);
        $rolePermissionsId = $role->permissions()->column('permission_id');
        $newPermIds = array_diff($perm_id, $rolePermissionsId);
        $result = $role->permissions()->attach($newPermIds);
        if (!$result)
            return json([
                'code' => 400,
                'msg' => '添加失败'
            ],400);
        return json([
            'code' => 200,
            'msg' => '添加成功'
        ]);
    }

    public function unassign_permissions(Request $request){
        $data = $request->param();

        //  验证参数
        $validate = new RoleAssignPerms();
        if (!$validate->check($data)) {
            return json([
                'code' => 400,
                'msg' => $validate->getError()
            ], 400);
        }

        $perm_id = $data['perm_id'];
        $role_id = $data['role_id'];
        $role = Role::find($role_id);
        if (!$role)
            return json([
                'code' => 404,
                'msg' => '添加失败'
            ],404);
        $result = $role->permissions()->detach($perm_id);
        if (!$result)
            return json([
                'code' => 400,
                'msg' => '删除失败'
            ]);
        return json([
            'code' => 200,
            'msg' => '删除成功'
        ]);
    }

    public function roles(Request $request){
        $perm_id = $request->param('perm_id');
        $perm = Permission::find($perm_id);
        if (!$perm)
            return json([
                'code' => 404,
                'msg' => '获取失败'
            ],404);
        $roles['id'] = $perm->permissions->column('id');
        $roles['name'] = $perm->permissions->column('name');
        $roles['description'] = $perm->permissions->column('description');
        if (!$roles)
            return json([
                'code' => 400,
                'msg' => '获取失败'
            ],400);
        return json([
            'code' => 200,
            'msg' => '获取成功',
            'data' => $roles
        ]);
    }

    public function perms(Request $request){
        $role_id = $request->param('role_id');
        $role = Role::find($role_id);
        if (!$role)
            return json([
                'code' => 404,
                'msg' => '获取失败'
            ],404);
        $perms['id'] = $role->permissions->column('id');
        $perms['name'] = $role->permissions->column('name');
        $perms['title'] = $role->permissions->column('title');
        if (!$perms)
            return json([
                'code' => 400,
                'msg' => '获取失败'
            ],400);
        return json([
            'code' => 200,
            'msg' => '获取成功',
            'data' => $perms
        ]);
    }
}